﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using FazaDemo.PL.AbstractPresenters;
using FazaDemo.ViewModels;
using FazaDemo.BL.InterfaceDomain;


namespace FazaDemo.PL.Wpf
{
    public class WorkAreaPresenter : IWorkAreaPresenter
    {

        private const string WorkArea = "WorkArea";

        private readonly IPresenter _presenter;

        private readonly IWorkAreaViewModel _viewModel;

        private WorkArea workArea;

        public WorkAreaPresenter(IPresenter presenter, IWorkAreaViewModel viewModel)
        {
            _presenter = presenter;
            _viewModel = viewModel;
        }

        public Window Initialize()
        {
            return _presenter.Initialize(new PresenterType { Name = WorkArea });
        }

        public void PrepareMenu(Window area)
        {
            workArea = area as WorkArea;
            if (workArea != null)
            {
                IEnumerable<Menu_Item> items = _viewModel.Items;
                IList<Menu_Item> writedItems = new List<Menu_Item>();
                BuildMenu(items, items, workArea.P_MainMenu, writedItems);
            }
        }

        private void BuildMenu(IEnumerable<Menu_Item> source, IEnumerable<Menu_Item> globalSource,
          object parent, IList<Menu_Item> writedItems)
        {
            foreach (var item in source)
            {
                item.NumEcq = 3054;
                Menu mainMenu = null;
                MenuItem curItem = null;
                if (parent is Menu)
                {
                    mainMenu = parent as Menu;
                }
                if (parent is MenuItem)
                {
                    curItem = parent as MenuItem;
                    curItem.Name = "i" + item.NumEcq;
                    curItem.Click += On_MenuItemClick;
                }
                if (writedItems.Where(i => i.Id == item.Id).ToList().Count == 0)
                {
                    object menuItem = null;
                    if (parent is Menu)
                    {
                        mainMenu.Items.Add(new MenuItem { Header = item.Text, Name = "i" + item.NumEcq });
                        menuItem = mainMenu.Items[mainMenu.Items.Count - 1];
                    }
                    if (parent is MenuItem)
                    {
                        curItem.Items.Add(new MenuItem { Header = item.Text, Name = "i" + item.NumEcq });
                        curItem.Click += On_MenuItemClick;
                        menuItem = curItem.Items[curItem.Items.Count - 1];
                    }
                    writedItems.Add(item);
                    var curChilds = globalSource.Where(s => s.ParentId == item.Id).ToList();
                    if (curChilds.Count > 0)
                    {
                        BuildMenu(curChilds, globalSource, menuItem, writedItems);
                    }
                }
            }
        }

        private void On_MenuItemClick(object sender, RoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            if (menuItem != null)
            {
                if (menuItem.Header.ToString() == "Адреса" && workArea.TabCtrlSupple.Items.Count == 0)
                {
                    IEnumerable<string> headers = new List<string> { "РАЙОНЫ", "НАСЕЛЁННЫЕ ПУНКТЫ", "УЛИЦЫ" };
                    ISuppleTabsPresenter tabsPresenter = new SuppleTabsPresenter(workArea);
                    tabsPresenter.BuildTabs(headers);
                }
                else
                {
                    workArea.Cont.Source = new Uri(("http://172.19.195.10:8580/Navigator/Page?numEcq=" + menuItem.Name.Substring(1)));   
                }
            }
        }

    }
}
